package com.basic.business.clickFarmingRecord.service.impl;

import com.basic.business.clickFarmingRecord.domain.TClickFarmingRecord;
import com.basic.business.clickFarmingRecord.dto.ClickFarmingDto;
import com.basic.business.clickFarmingRecord.mapper.TClickFarmingRecordMapper;
import com.basic.business.clickFarmingRecord.service.ITClickFarmingRecordService;
import com.basic.business.goodsIndicators.domain.TGoodsIndicators;
import com.basic.business.goodsIndicators.mapper.TGoodsIndicatorsMapper;
import com.basic.business.order.mapper.TOrdersMergeMapper;
import com.basic.common.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 刷单数据记录Service业务层处理
 *
 * @author zhaoxy_jn@163.com
 */
@Service
public class TClickFarmingRecordServiceImpl extends BaseServiceImpl<TClickFarmingRecordMapper, TClickFarmingRecord> implements ITClickFarmingRecordService {

    @Resource
    private TClickFarmingRecordMapper tClickFarmingRecordMapper;

    @Resource
    private TGoodsIndicatorsMapper tGoodsIndicatorsMapper;

    @Resource
    private TOrdersMergeMapper tOrdersMergeMapper;

    @Override
    public void handleClickFarmingRecord() {

        //获取刷单未处理数据
        List<TClickFarmingRecord> recordList = tClickFarmingRecordMapper.getNormalRecordList();

        if (recordList!=null && !recordList.isEmpty()){
            for (TClickFarmingRecord record : recordList) {
                TGoodsIndicators goodsIndicators = tGoodsIndicatorsMapper.getByGoodsIdAndStatDate(record.getGoodsId(), record.getStatDate());
                if (goodsIndicators != null){
                    goodsIndicators.setPayBuyerNumber(goodsIndicators.getPayBuyerNumber()-1);
                    goodsIndicators.setPayAmount(goodsIndicators.getPayAmount().subtract(record.getPayAmount()));
                    tGoodsIndicatorsMapper.update(goodsIndicators);

                    //设置为已处理
                    record.setStatus("DELETED");
                    tClickFarmingRecordMapper.update(record);
                }
            }
        }
    }

    @Override
    public void clickFarmingTask() {

        //获取刷单数据
        List<ClickFarmingDto> list = tOrdersMergeMapper.getClickFarmingData();

        if (!list.isEmpty()){
            handleGoodsData(list);
        }
    }

    public void handleGoodsData(List<ClickFarmingDto> list){

        for (ClickFarmingDto clickFarmingDto : list){
            TGoodsIndicators goodsIndicators = tGoodsIndicatorsMapper.getByGoodsIdAndStatDate(clickFarmingDto.getNumIid(), clickFarmingDto.getPayTime());
            if (goodsIndicators != null){
                goodsIndicators.setPayBuyerNumber(goodsIndicators.getPayBuyerNumber()-1);
                goodsIndicators.setPayAmount(goodsIndicators.getPayAmount().subtract(clickFarmingDto.getPayAmount()));
                tGoodsIndicatorsMapper.update(goodsIndicators);

                //存储处理完毕的刷单数据
                TClickFarmingRecord record = new TClickFarmingRecord();
                record.setId(null);
                record.setGoodsId(clickFarmingDto.getNumIid());
                record.setPayAmount(clickFarmingDto.getPayAmount());
                record.setStatDate(clickFarmingDto.getPayTime());
                record.setStatus("DELETED");

                tClickFarmingRecordMapper.insert(record);

            }else {
                //存储未处理的刷单数据
                TClickFarmingRecord record = new TClickFarmingRecord();
                record.setId(null);
                record.setGoodsId(clickFarmingDto.getNumIid());
                record.setPayAmount(clickFarmingDto.getPayAmount());
                record.setStatDate(clickFarmingDto.getPayTime());
                record.setStatus("NORMAL");

                tClickFarmingRecordMapper.insert(record);
            }
        }

    }
}
